<?php

namespace api\wxapp\model;

use think\Model;
use api\wxapp\model\ShopGoods;
use api\wxapp\model\User;
use api\wxapp\model\OrderList;
use api\wxapp\model\GoodsActivity;
class Cart extends Model
{
    protected $table = 'sssc_app_cart';

    //关联用户表
    public function leftuser($query)
    {
        return $query->join('app_user t3','t3.id = t1.uid');
    }
    //关联商品表
    public function leftgoods($query)
    {
        // 按照创建时间排序
        return $query->join('goods t2','t2.id = t1.goods_id');
    }
    //关联店铺
    public function leftshop($query)
    {
        // 按照创建时间排序
        return $query->join('app_shop t4','t4.id = t3.shop_id');
    }
    //字段设置
    public function returnfield($query)
    {
        return $query->field('t1.id,t1.goods_num,t1.goods_id,t2.img,t2.name,t2.price,t2.old_price');
    }
    //购物车列表
    public function getList($params)
    {
        $where[] = ['t1.uid','eq',$params['uid']];
        !isset($params['type']) ? $type = 1 : $type = $params['type'];
        //查询当前用所属的店铺
        $shop_id = User::where('id',$params['uid'])->value('shop_id');
        $goods_id = ShopGoods::where('shop_id',$shop_id)->column('goods_id');
        //默认是直邮的商品
        if($type == 1){
            $where[] = ['t1.goods_id','NOTIN',$goods_id];
        }else{
            $where[] = ['t1.goods_id','in',$goods_id];
        }
        $query = self::alias('t1');
        $query = $this->leftgoods($query);
        $query = $this->returnfield($query);
        $data = $query->where($where)->select()->toArray();
        
        
        $map[] = ['etime','egt',date('Y-m-d H:i:s',time())];
        $map[] = ['stime','elt',date('Y-m-d H:i:s',time())];
        $map[] = ['shop_id','eq',$shop_id];
        $activity = GoodsActivity::where($map)->find();
        $arr = json_decode($activity['value'],true);
        $real = array();
        $noreal = array();
        foreach($data as $k=>$v){
            $data[$k]['total'] = OrderList::where('goods_id',$v['goods_id'])->SUM('num');
            if($type == 2){//店售商品
                $data[$k]['shop_id'] = $shop_id;
                $data[$k]['lable'] = '自提';
                $find = ShopGoods::where(array('shop_id'=>$shop_id,'goods_id'=>$v['goods_id']))->find();
                $data[$k]['old_price'] = $v['price'];
                if(isset($arr[$v['goods_id']])){
                    //存在是秒杀商品
                    $data[$k]['price'] = $arr[$v['goods_id']];
                    $data[$k]['is_activity'] = $activity['id'];
                    $real[] = $data[$k];
                }else{
                    $data[$k]['price'] = $find['price'];
                    //判断 当前商品库存是否  为0
                    if($find['num'] <= 0){
                        $noreal = $data[$k];
                    }else{
                        $real[] = $data[$k];
                    }
                }
            }else{
                $data[$k]['lable'] = '直邮';
                //直 直邮商品 不过期
                $real[] = $data[$k];
            }
        }
        return array('real'=>$real,'noreal'=>$noreal);
    }

    //修改购物车 商品
    
}
